package com.example.spring.demo.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.spring.demo.common.error.CustomException;
import com.example.spring.demo.entity.GoodsCategory;
import com.example.spring.demo.common.model.req.GoodsCategoryParam;
import com.example.spring.demo.common.model.vo.SelectType;
import com.example.spring.demo.service.GoodsCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/api/category")
public class GoodsCategoryController {

    @Autowired
    private GoodsCategoryService goodsCategoryService;

    //新增
    @PostMapping
    public String add(@RequestBody GoodsCategory goodsCategory) {
        boolean temp = goodsCategoryService.save(goodsCategory);
        if (temp) {
            return "新增成功!";
        }
        throw new CustomException("新增失败!");
    }

    //编辑
    @PutMapping
    public String edit(@RequestBody GoodsCategory goodsCategory) {
        boolean temp = goodsCategoryService.updateById(goodsCategory);
        if (temp) {
            return "编辑成功!";
        }
        throw new CustomException("编辑失败!");
    }

    //删除
    @DeleteMapping("/{categoryId}")
    public String delete(@PathVariable("categoryId") Long categoryId) {
        if (goodsCategoryService.removeById(categoryId)) {
            return "删除成功!";
        }
        throw new CustomException("删除失败!");
    }

    //列表
    @GetMapping("/list")
    public IPage<GoodsCategory> list(GoodsCategoryParam param) {
        //构造查询条件
        QueryWrapper<GoodsCategory> query = new QueryWrapper<>();
        query.lambda().like(StrUtil.isNotEmpty(param.getCategoryName()),
                        GoodsCategory::getCategoryName, param.getCategoryName())
                .orderByDesc(GoodsCategory::getOrderNum);//desc 降序  asc 升序
        //构造分页对象
        IPage<GoodsCategory> page = new Page<>(param.getCurPage(), param.getPageSize());
        //查询数据
        return goodsCategoryService.page(page, query);
    }

    //分类列表
    @GetMapping("/getSelectList")
    public List<SelectType> getSelectList() {
        //查询分类列表
        QueryWrapper<GoodsCategory> query = new QueryWrapper<>();
        query.lambda().orderByAsc(GoodsCategory::getOrderNum);
        List<GoodsCategory> list = goodsCategoryService.list(query);
        //存储小程序需要的类型
        List<SelectType> selectList = new ArrayList<>();
        //构造小程序需要的类型
        Optional.ofNullable(list).orElse(new ArrayList<>())
                .forEach(item -> {
                    SelectType type = new SelectType();
                    type.setLabel(item.getCategoryName());
                    type.setValue(item.getCategoryId());
                    selectList.add(type);
                });
        return selectList;
    }

    //小程序分类接口
    @GetMapping("/getCateList")
    public List<GoodsCategory> getCateList(){
        return goodsCategoryService.list();
    }
}
